#include "combodelegate.h"
#include <QComboBox>
#include <QDebug>

ComboDelegate::ComboDelegate(QObject *parent) :
    QItemDelegate(parent)
{
}

QWidget * ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/, const QModelIndex &/*index*/) const
{
    QComboBox *comboBox = new QComboBox(parent);
    comboBox->insertItem(0,"Test1");
    comboBox->insertItem(1,"Test2");
    comboBox->insertItem(2,"Test3");
    comboBox->insertItem(3,"Test4");
    comboBox->insertItem(4,"Test5");
    comboBox->insertItem(5,"Test6");
    comboBox->insertItem(6,"Test7");
    comboBox->insertItem(7,"Test8");
    comboBox->insertItem(8,"Test9");
    comboBox->insertItem(9,"Test10");
    return comboBox;
}

void ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    int value = index.model()->data(index, Qt::EditRole).toInt();
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
    comboBox->setCurrentIndex(value);
}

void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QComboBox *comboBox = static_cast<QComboBox*>(editor);

    int value = comboBox->currentIndex();

    model->setData(index, value, Qt::EditRole);
}

void ComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
{
    editor->setGeometry(option.rect);
}
